package com.lewei.print;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

import com.lewei.utils.Constants;

/**
 * 
 * SGB数据打印模型
 * 
 * @author
 * 
 */
public class SGB02 implements Printable {
	/** 标签样式 */
	private String lenFlag = "SGB";
	/** 试验单位 */
	
	// ==============================================
	/** 日期 */
	private String prodDatea = "2017-02-09";

	/** 编号 * SGB+ () + - + 904373G + () */
	private String typeNo = "";
	
	private String typeNo1 = "";
	/** 出厂编号 */
	private String serialNo = "200052-951";

	/** 物料类别前 5位 SGB01 */
	private String elevatorNo = "SGB01";

	/** Fmax 客户合同号中的guu 1千克(kg)=2.2046226磅(lb) */
	private String Fmax = "";
	private String Fmlb = "";

	/**
	 * F 1350-3600(干) 1780-3060(湿) 1千克(kg)=2.2046226磅(lb)
	 * 2976.2405395-7936.6414387 3924.2282669-6746.1452229
	 */
	private String F = "";
	private String Flb = "";
	
	/** Vmax 客户合同中的Vmax 1FPM(ft/min)=0.00508m/s */
	private String Vmax = "";
	private String Vmfpm = "";

	/** Vkn 客户合同中的Vkn */
	private String Vkn = "";
	private String Vkfpm = "";

	private String k = "";
	private String a = "";

	public String getA() {
		return a;
	}

	public void setA(String a) {
		this.a = a;
	}

	/**
	 * 客户合同号中 干 湿
	 */
	private String Guidrail = "";

	/** 左边距 */
	private double paddingLeft = 10;

	/** 上边距 */
	private double paddingTop = 20;

	/** 打印机名称 */
	private String printerName = "Foxit Reader Plus Printer";

	
	public String getFmlb() {
		return Fmlb;
	}

	public void setFmlb(String fmlb) {
		Fmlb = fmlb;
	}

	public String getFlb() {
		return Flb;
	}

	public void setFlb(String flb) {
		Flb = flb;
	}

	public String getVmfpm() {
		return Vmfpm;
	}

	public void setVmfpm(String vmfpm) {
		Vmfpm = vmfpm;
	}

	public String getVkfpm() {
		return Vkfpm;
	}

	public void setVkfpm(String vkfpm) {
		Vkfpm = vkfpm;
	}

	public String getTypeNo() {
		return typeNo;
	}

	public void setTypeNo(String typeNo) {
		this.typeNo = typeNo;
	}

	public String getLenFlag() {
		return lenFlag;
	}

	public void setLenFlag(String lenFlag) {
		this.lenFlag = lenFlag;
	}

	public String getProdDatea() {
		return prodDatea;
	}

	public void setProdDatea(String prodDatea) {
		this.prodDatea = prodDatea;
	}

	public String getSerialNo() {
		return serialNo;
	}

	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}

	public String getElevatorNo() {
		return elevatorNo;
	}

	public void setElevatorNo(String elevatorNo) {
		this.elevatorNo = elevatorNo;
	}

	public String getFmax() {
		return Fmax;
	}

	public void setFmax(String fmax) {
		Fmax = fmax;
	}

	public String getF() {
		return F;
	}

	public void setF(String f) {
		F = f;
	}

	public String getVmax() {
		return Vmax;
	}

	public void setVmax(String vmax) {
		Vmax = vmax;
	}

	public String getVkn() {
		return Vkn;
	}

	public void setVkn(String vkn) {
		Vkn = vkn;
	}

	public String getK() {
		return k;
	}

	public void setK(String k) {
		this.k = k;
	}

	public String getGuidrail() {
		return Guidrail;
	}

	public void setGuidrail(String guidrail) {
		Guidrail = guidrail;
	}

	public double getPaddingLeft() {
		return paddingLeft;
	}

	public void setPaddingLeft(double paddingLeft) {
		this.paddingLeft = paddingLeft;
	}

	public double getPaddingTop() {
		return paddingTop;
	}

	public void setPaddingTop(double paddingTop) {
		this.paddingTop = paddingTop;
	}

	public String getPrinterName() {
		return printerName;
	}

	public void setPrinterName(String printerName) {
		this.printerName = printerName;
	}

	/**
	 * 设置边距。
	 * 
	 * @param paddingLeft
	 * @param paddingTop
	 */
	public void setPadding(double paddingLeft, double paddingTop) {
		this.paddingLeft = paddingLeft;
		this.paddingTop = paddingTop;
	}

	public SGB02(String printerName) {
		super();
		this.printerName = printerName;
	}

	public SGB02() {
		super();
	}

	/**
	 * @param Graphic指明打印的图形环境
	 * @param PageFormat指明打印页格式
	 *            （页面大小以点为计量单位，1点为1英才的1/72，1英寸为25.4毫米。A4纸(210mm×297mm)大致为595×
	 *            842点）比例约等于2.83
	 * @param pageIndex指明页号
	 **/

	@Override
	public int print(Graphics gra, PageFormat pf, int pageIndex)
			throws PrinterException {
		// TODO Auto-generated method stub
		Component c = null;
		// 转换成Graphics2D
		Graphics2D g2 = (Graphics2D) gra;
		// 设置打印颜色为黑色
		g2.setColor(Color.black);

		// 打印起点坐标
		double x = pf.getImageableX() + 60;
		double y = pf.getImageableY();

		switch (pageIndex) {
		case 0:
				Font font = new Font("Calibri", Font.BOLD, 11);
				g2.setFont(font);
				g2.drawString(typeNo, (float) (x + 10), (float) (y + 20));//头编码
				String filename = "C:\\toolsZ\\labelPrint\\codeZ\\typeNo.jpg";
				Image Img = null;
				try {
					Img = ImageIO.read(new File(filename));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
//				g2.drawImage(Img, (int) x + 125, (int) y + 15, 55, 55, c);//头编码二维码
				font = new Font("Calibri", Font.BOLD, 9);
				g2.setFont(font);
				g2.drawString(serialNo, (float) (x + 25), (float) (y + 44));// 中间的出厂编号C180066_111
				filename = "C:\\toolsZ\\labelPrint\\codeZ\\serialNo.jpg";
				try {
					Img = ImageIO.read(new File(filename));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g2.drawImage(Img, (int) x + 117, (int) y + 27, 55, 55, c);// 中间的出厂编号200066_XXX二维码
				font = new Font("Calibri", Font.BOLD, 11);
				g2.setFont(font);
				g2.drawString(elevatorNo, (float) (x + 45), (float) (y + 68));//elevatorNo SGB01 

				font = new Font("Calibri", Font.BOLD, 10);
				g2.setFont(font);
				g2.drawString(F, (float) (x+5), (float) (y + 92));//F
				g2.drawString(Flb, (float) (x+5), (float) (y + 110));//Flb
				g2.drawString(Fmax, (float) (x + 117), (float) (y + 92));//Fmax
				g2.drawString(Fmlb, (float) (x + 117), (float) (y + 110));//Fmlb
				
//				g2.drawString("1111", (float) (x+5), (float) (y + 128));//测试
				
				g2.drawString(Vmax, (float) (x+5),(float) (y + 147));//Vmax
				g2.drawString(Vmfpm, (float) (x+5), (float) (y + 166));// Vmfpm
				g2.drawString(Vkn, (float) (x + 117), (float) (y + 147));// Vkn
				g2.drawString(Vkfpm, (float) (x + 117), (float) (y + 166));//Vkfpm
				
//				g2.drawString("1111", (float) (x+5), (float) (y + 182));//测试1111
				
//				font = new Font("Calibri", Font.BOLD, 9);
//				g2.setFont(font);
				g2.drawString(k, (float) (x + 45), (float) (y + 202));//k
				
				if(Guidrail.equals("干燥")){
					filename = "C:\\toolsZ\\labelPrint\\codeZ\\dry.JPG";
				}else if(Guidrail.equals("润滑")){
					filename = "C:\\toolsZ\\labelPrint\\codeZ\\oiled.jpg";
				}
				
				try {
					Img = ImageIO.read(new File(filename));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				g2.drawImage(Img, (int) x + 84, (int) y + 214, 16, 16, c);// 
				g2.drawString("B", (float) (x + 50), (float) (y + 229));//B
				g2.drawString(a, (float) (x + 45), (float) (y + 246));//A
				
				font = new Font("Calibri", Font.BOLD, 6);
				g2.setFont(font);
				g2.drawString(prodDatea, (float) (x - 42), (float) (y + 22));// 左侧左上角时间

			return PAGE_EXISTS;
		default:
			return NO_SUCH_PAGE;
		}
	}

	public static void main(String[] args) {

		// 通俗理解就是书、文档
		Book book = new Book();
		// 设置成竖打
		PageFormat pf = new PageFormat();
		pf.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		p.setSize(500,500);// 纸张大小
		// A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		p.setImageableArea(0, 0, 500, 500);
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(new SGB02(), pf);

		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();

		HashAttributeSet hs = new HashAttributeSet();

		String printerName = "HP LaserJet Professional P1106";
		hs.add(new PrinterName(printerName, null));

		PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);

		try {
			job.setPrintService(pss[0]);
		} catch (PrinterException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 设置打印类
		job.setPageable(book);

		try {
			job.print();
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	public void printcode() {

		// 通俗理解就是书、文档
		Book book = new Book();

		PageFormat pageFormat = new PageFormat();
		// 打印方向
		pageFormat.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper paper = new Paper();
		paper.setSize(500, 500);// 纸张大小
		// if(lenFlag.equals("E")){
		// // A4(595 X 842)设置打印区域，其实0，0应该是72，72，因为A4纸的默认X,Y边距是72
		// paper.setImageableArea(Constants.LABELLEFT900540E,
		// Constants.LABELTOP900540E, 400, 300);
		// }else if(lenFlag.equals("G")&&modelNo.equals("900540")){
		// paper.setImageableArea(Constants.LABELLEFT900540-3,
		// Constants.LABELTOP900540-5, 400, 300);
		// }else{
		paper.setImageableArea(Constants.LABELLEFTSGB,Constants.LABELTOPSGB, 500, 500);
		// }

		pageFormat.setPaper(paper);
		// 把 PageFormat 和 Printable 添加到书中，组成一个页面
		book.append(this, pageFormat);

		// 获取打印服务对象
		PrinterJob printJob = PrinterJob.getPrinterJob();

		HashAttributeSet hashAttributeSet = new HashAttributeSet();

		hashAttributeSet.add(new PrinterName(printerName, null));

		PrintService[] printServices = PrintServiceLookup.lookupPrintServices(
				null, hashAttributeSet);

		try {
			printJob.setPrintService(printServices[0]);
		} catch (PrinterException e1) {
			e1.printStackTrace();
			throw new RuntimeException(e1.getMessage());
		}

		// 设置打印类
		printJob.setPageable(book);
		try {
			// 可以用printDialog显示打印对话框，在用户确认后打印；也可以直接打印
			// boolean a=job.printDialog();
			// if(a)
			// {
			printJob.print();
			// }
		} catch (PrinterException e) {
			e.printStackTrace();
		}

	}

	public String getTypeNo1() {
		return typeNo1;
	}

	public void setTypeNo1(String typeNo1) {
		this.typeNo1 = typeNo1;
	}

}
